<?php
namespace Home\Controller;
use Think\Controller;
class UserApiController extends Controller {
    public function index(){
       return false;
    }

    public function _empty(){
        header("HTTP/1.0 404 Not Found");//使HTTP返回404状态码 
    }

    /**
     * [ 用户注册]
     * @return [json] [状态码]
     */
    public function reg(){
        $regType= M('system_config')->getByConfig('RegType');
        if($regType['value']!=0){
            $data['mobile']=I('mobile');
            $data['password']=I('password');
            $data['Repassword']=I('repassword');
            $data['VerifyCode']=I('valicode');
            $time=I('time');
            $token=I('token');

            if($data['mobile']==''){
                echo json_encode(array('status'=>-3));//手机为空
            }elseif($data['password']==''){
                echo json_encode(array('status'=>-4));//密码为空
            }elseif($data['VerifyCode']==''){
                echo json_encode(array('status'=>-9));//验证码为空
            }elseif($time==''){
                echo json_encode(array('status'=>-5));//time为空
            }elseif($token==''){
                echo json_encode(array('status'=>-6));//token为空
            }else{
                $token2=md5($time.md5(C('PWD_SALT')).'reg');
                $Mpass=md5(C('PWD_SALT').$password);
                if($token2!=$token){
                    echo json_encode(array('status'=>-7));//token错误
                    exit();
                }else{
                    $reg=M('User_info')->where(array('mobile'=>$data['mobile']))->join('__USER__ on __USER_INFO__.uid=__USER__.uid')->select();
                    if(count($reg)<=0){
                        $userModel=D('User');
                        $userModel->startTrans();
                        $data['username'] = 'm'.time();
                        $data['password']=$Mpass;
                        $data['lastlogintime']=time();
                        $data['lastloginip']=I('server.REMOTE_ADDR');
                        $redata = $userModel->create($data);
                        if($redata){
                            $res=$res->add();
                            $dataGet['uid'] = $res;
                            $dataGet['mobile'] = $data['mobile'];
                            if(M('UserInfo')->data($dataGet)->add()){
                                $userModel->commit();
                                echo json_encode(array('status'=>1,'userid'=>$res));//正常
                            }else{
                                $userModel->rollback();
                                echo json_encode(array('status'=>-1));//数据库异常
                            }
                        }else{  
                            $userModel->rollback();
                            echo urldecode($userModel->getError());
                            echo json_encode(array('status'=>-10,'info'=>$userModel->getError()));//写入异常

                        }
                    }else{
                        echo json_encode(array('status'=>-2,'userinfo'=>$reg));//手机号存在
                    }
                }   
            }
        }else{
            echo json_encode(array('status'=>-8));//注册关闭
        }
    }

    /**
     * [验证手机号]
     * @param [String] $mobile [手机号]
     * @return [json] [状态码]
     */
    public function checkmobile($mobile="",$token="",$time=""){
        if($mobile==''){
            echo json_encode(array('status'=>-3));//手机为空
        }elseif($time==''){
            echo json_encode(array('status'=>-5));//time为空
        }elseif($token==''){
            echo json_encode(array('status'=>-6));//token为空
        }else{
            $token2=md5($time.md5(C('PWD_SALT')).'checkmobile');
            if($token2!=$token){
                echo json_encode(array('status'=>-7));//token错误
            }else{
                $reg=M('User_info')->where(array('mobile'=>$mobile))
                ->join('__USER__ on __USER_INFO__.uid=__USER__.uid')
                ->find();
                if(count($reg)>0){
                    echo json_encode(array('status'=>-2,'userinfo'=>$reg));//手机号已经存在；
                }else{
                    echo json_encode(array('status'=>1));//手机号不存在；
                }
            }
        }
    }

     /**
     * [验证用户名]
     * @param [String] $username [用户名]
     * @return [json] [状态码]
     */
    public function checkusername($username="",$token="",$time=""){
        if($username==''){
            echo json_encode(array('status'=>-3));//用户名为空
        }elseif($time==''){
            echo json_encode(array('status'=>-5));//time为空
        }elseif($token==''){
            echo json_encode(array('status'=>-6));//token为空
        }else{
            $token2=md5($time.md5(C('PWD_SALT')).'checkusername');
            if($token2!=$token){
                echo json_encode(array('status'=>-7));//token错误
            }else{
                $reg=M('User')->where(array('username'=>$username))
                ->join('__USER_INFO__ on __USER_INFO__.uid=__USER__.uid')
                ->find();
                if(count($reg)>0){
                    echo json_encode(array('status'=>-2,'userinfo'=>$reg));//用户名已经存在；
                }else{
                    echo json_encode(array('status'=>1));//用户名不存在；
                }
            }
        }
    }

    /*public function usernameLogin($username,$PasswWord){
        if($username==""){
            echo json_encode(array('status'=>-3));//用户名为空
            exit();
        }elseif($PasswWord==""){
            echo json_encode(array('status'=>-4));//密码为空
            exit();
        }else{
            $Mpass=md5($PasswWord.C('PWD_SALT'));
            $userinfo=M('User')
                ->where(array('username'=>$username))
                ->join('__USER_INFO__ on __USER__.uid=__USER_INFO__.uid')
                ->find();
            if(count($userinfo)<=0){
                echo json_encode(array('status'=>-1)); //用户名不存在
            }elseif($userinfo['password']!=$Mpass){
                echo json_encode(array('status'=>-2)); //密码错误
            }else{
                echo json_encode(array('status'=>1,'userinfo'=>$userinfo)); //用户名密码错误
            }           
        }
    }


    public function mobileLogin($mobile,$PasswWord){
        if($mobile==""){
            echo json_encode(array('status'=>-3));//手机为空
        }elseif($PasswWord==""){
            echo json_encode(array('status'=>-4));//密码为空
        }else{
            $Mpass=md5($PasswWord.C('PWD_SALT'));
            $userinfo=M('User_info')
                ->where(array('mobile'=>$mobile))
                ->join('__USER__ on __USER__.uid=__USER_INFO__.uid')
                ->find();
            if(count($userinfo)<=0){
                echo json_encode(array('status'=>-1)); //手机不存在
            }elseif($userinfo['password']!=$Mpass){
                echo json_encode(array('status'=>-2)); //密码错误
            }else{
                echo json_encode(array('status'=>1,'userinfo'=>$userinfo)); //返回用户信息
            }           
        }
    }*/


}